using System;
using System.Collections.Generic;
using Game1942.Library;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game1942
{
    public class MenuComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {

        #region Variables
        private int elementActive = 0;
        private List<string> elements;
        private readonly Color selectedColor;
        private readonly Color unselectedColor;
        private Vector2 freePosition = new Vector2(0, 0);
        private readonly SpriteFont font;
        private Boolean keyUpPress;
        private Boolean keyDownPress;
        private readonly int separation;
        private SoundEffect selectSound;
        private Texture2D menuPic;
        #endregion

        public MenuComponent(Microsoft.Xna.Framework.Game game, Color selectedC, Color unselectedC, Vector2 freePos, SpriteFont _font, int _separation,
            ContentManager contentManager)
            : base(game)
        {
            elements = new List<string>();
            selectedColor = selectedC;
            unselectedColor = unselectedC;
            freePosition = freePos;
            font = _font;
            separation = _separation;
            selectSound = contentManager.Load<SoundEffect>("Sounds/Select");
            menuPic = contentManager.Load<Texture2D>("Backgrounds/MenuPic");
        }

        public void AddElement(string elementName)
        {
            elements.Add(elementName);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(menuPic, new Rectangle(0, 0, Constants.Screen.Width, Constants.Screen.Height), Color.White);
            for (int i = 0; i < elements.Count; i++)
            {
                if (elementActive == i)
                {
                    spriteBatch.DrawString(font, elements[i].ToString(), new Vector2(freePosition.X, freePosition.Y + (separation * i)), selectedColor);
                }
                else
                {
                    spriteBatch.DrawString(font, elements[i].ToString(), new Vector2(freePosition.X, freePosition.Y + (separation * i)), unselectedColor);
                }
            }

            spriteBatch.End();
        }

        private void NextItem()
        {
            elementActive++;
            if (elementActive == elements.Count)
            {
                elementActive = 0;
            }
        }

        private void PreviousItem()
        {
            
            if (elementActive > 0)
            {
                elementActive--;
            }
            else
            {
                elementActive = elements.Count - 1;
            }
        }

        public int ElementActive()
        {
            return elementActive + 1;
        }


        public void PressKeys(KeyboardState keys)
        {
            if (keys.IsKeyDown(Keys.Up) && keyUpPress)
            {
                PreviousItem();
                keyUpPress = false;
                selectSound.Play();
            }
            else
            {
                if (keys.IsKeyUp(Keys.Up))
                {
                    keyUpPress = true;
                }
            }

            if (keys.IsKeyDown(Keys.Down) && keyDownPress)
            {
                NextItem();
                keyDownPress = false;;
                selectSound.Play();
            }
            else
            {
                if (keys.IsKeyUp(Keys.Down))
                {
                    keyDownPress = true;
                }
            }
        }
    }
}